package com.hythzx.security;

import com.hythzx.domain.User;
import com.hythzx.repository.UserRepository;
import com.hythzx.service.dto.UserDTO;
import com.hythzx.service.mapper.UserMapper;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by zhaoy on 2017/3/3.
 */
@Component("userDetailsService")
public class UserDetailServiceImpl implements UserDetailsService {

    private final UserRepository  userRepository;
    private final UserMapper userMapper;

    public UserDetailServiceImpl(UserRepository userRepository, UserMapper userMapper) {
        this.userRepository = userRepository;
        this.userMapper = userMapper;
    }

    @Override
    @Transactional
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        User user = userRepository.findTop1ByLoginName(s);
        UserDTO userDTO = userMapper.userToUserDTO(user);
        return userDTO;
    }
}
